package com.cjt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjt.domain.Student;
import com.cjt.service.StudentService;
import com.cjt.utils.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjt
 * @since 2020-10-26
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;

    @RequestMapping("/getAll")
    public Object selectAll() {
        return studentService.list(null);
    }

    @RequestMapping("/selectByPage")
    public Object selectByPage(Student stu ,Page<Student> page ) {
        System.out.println(page.getCurrent());
        return studentService.selectByPage(page,stu);
    }
    @RequestMapping("/getAllVue")
    public Object getAllVue(Page<Student> page) {
        return new Result(1, "成功", studentService.page(page), studentService.count());
    }

    @RequestMapping("/update")
    public Object update(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    @RequestMapping("/add")
    public Object add(@RequestBody Student student) {
        System.out.println(student);
        Student Name = studentService.getOne(new QueryWrapper<Student>().eq("name", student.getName()));//使用mybatis plus条件构造器queryWrapper，根据 entity 条件，查询一条记录
        if (Name != null) {
            return new Result(0, "添加失败，该学生已存在！");//把查到的结果数据，返回给前端ajax的请求
        }
        return studentService.save(student);
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody Student student) {
        return studentService.removeById(student.getId());
    }
}
